import Router from '@koa/router';
import db from '../mysqlHelper.js';
import logger from '../logger.js';
import response from '../response.js';

import redis from '../redis.js';
import moment from 'moment';
import { GetOpenID } from '../common/wx.js';
import util from '../common/util.js';

const router = new Router();
router.prefix('/api');



//接收图片并上传到阿里云OSS
router.post('/upload/image', async (ctx) => {
    const file = ctx.request.files.file; // 获取上传文件
//获取上传文件的数组
    if (file == null) {
        ctx.body = response.fail('参数不允许为空');
        return;
    }
    if(ctx.request.body.ossPath == null){
        ctx.body = response.fail('缺少必填字段 ossPath');
        return;
    }
    let type ='public';
    if(ctx.request.body.type){
        type = ctx.request.body.type;
    }
    //获取流里的数组
    let buffer = fs.readFileSync(file.filepath);
    let result = await uploadFile(ctx.request.body.ossPath, buffer,type);
    if (result.code == 0) {
        ctx.body = response.fail(result.msg);
        return;
    }
    ctx.body = response.success(result, '上传成功');
});


export { router };
